<%@ Page language="vb" CodeFile="activateInvoiceCredit.aspx.vb" Inherits="activateInvoiceCredit_aspx_vb" %>

<%
    rowNumber = 1
    invoiceCreditID = Request.QueryString("invoiceCreditID")
    If Len(invoiceCreditID) > 0 Then 
        invoiceCreditID = CLng(invoiceCreditID)
    End If
    If (Request.QueryString("do")) = Nothing Then
        subDo = Request.QueryString("subDo")
        If Len(invoiceCreditID) > 0 Then
            dbOpen()
            RecSet = Conn.Execute("Select ic.paymentTerm, p.partnerName From (tblInvoiceCredit ic INNER JOIN tblPartner p ON p.partnerID = ic.partnerID) Where invoiceCreditID = " & invoiceCreditID & "")
            If RecSet.EOF Then
                Response.Redirect("activateInvoiceCredit.aspx")
            Else
                paymentTerm = RecSet.Fields("paymentTerm").Value
                partnerName = RecSet.Fields("partnerName").Value
            End If
            RecSet.Close()
            RecSet = Nothing
            dbClose()
        End If
        Over()
        strJS = ""
        strJS = strJS + "[""invoiceDateYear"", ""<1""],"
        strJS = strJS + "[""invoiceDateMonth"", ""<1""],"
        strJS = strJS + "[""invoiceDateDay"", ""<1""]"
        checkFields()
        realTime = Now()
        year = Left(realTime, 4)
        month = Mid(realTime, 6, 2)
        day = Mid(realTime, 9, 2)
        Response.Write("<table cellpadding='0' cellspacing='0' border='0' width='1000'>")
        Response.Write("<tr>")
        Response.Write("<td valign='top'>")
        Response.Write("<table cellpadding='2' cellspacing='2' width='450'>")
        Response.Write("<tr><td class='headline' colspan='3'>ACTIVATE AN CREDIT INVOICE<font color='#fcbc00'>" & subDo & "</font><hr class='hrHeadline'></td></tr>")
        Response.Write("<form action='activateInvoiceCredit.aspx?do=save&invoiceCreditID=" & invoiceCreditID & "' method='post' onsubmit=""javascript:return checkFields();"">")
        Response.Write("<tr><td class='formBold'>Invoice ID:</td><td align='right'><input type='text' name='invoiceCreditID' id='invoiceCreditID' value='" & invoiceCreditID & "' size='40' maxlength='50' readonly='readonly'></td></tr>")
        Response.Write("<tr><td class='formBold'>Partner:</td><td align='right'><input type='text' name='partnerName' id='partnerName' value='" & partnerName & "' size='40' maxlength='50' readonly='readonly'></td></tr>")
        Response.Write("<tr><td class='formBold'>Payment term (days):</td><td align='right'><input type='text' name='paymentTerm' id='paymentTerm' value='" & paymentTerm & "' size='40' maxlength='50' onkeyup='re=/(\d*)/; re.exec(this.value); this.value=RegExp.$1;'></td></tr>")
        Response.Write("<tr>")
        Response.Write("<td class='formBold'>Invoice date:&nbsp; </td>")
        Response.Write("<td align='right'>")
        Response.Write("<select name='invoiceDateYear' id='invoiceDateYear'>")
        Response.Write("<option value=''>&nbsp; Year ")
        stopYear = CDbl(year) + 3
        yearCounter = year
        For yearLoop = year To stopYear
            Response.Write("<option value='" & yearCounter & "'")
            If CStr(yearCounter) = year Then
                Response.Write("selected='selected'")
            End If
            Response.Write("> " & yearCounter & " ")
            yearCounter = yearCounter + 1
        Next
        Response.Write("</select>")
        Response.Write(" - ")
        Response.Write("<select name='invoiceDateMonth' id='invoiceDateMonth'>")
        Response.Write("<option value=''>&nbsp; Month ")
        yearCounter = 1
        For yearLoop = 1 To 12
            Response.Write("<option value='" & yearCounter & "'")
            If CLng(yearCounter) = CLng(month) Then
                Response.Write("selected='selected'")
            End If
            Response.Write("> " & yearCounter & " ")
            yearCounter = yearCounter + 1
        Next
        Response.Write("</select>")
        Response.Write(" - ")
        Response.Write("<select name='invoiceDateDay' id='invoiceDateDay'>")
        Response.Write("<option value=''>&nbsp; Day&nbsp; ")
        yearCounter = 1
        For yearLoop = 1 To 31
            Response.Write("<option value='" & yearCounter & "'")
            If CLng(yearCounter) = CLng(day) Then
                Response.Write("selected='selected'")
            End If
            Response.Write("> " & yearCounter & " ")
            yearCounter = yearCounter + 1
        Next
        Response.Write("</select>")
        Response.Write("</td>")
        Response.Write("<td>")
        Response.Write("<div id='error_invoiceDateYear' class='edit_error'>&laquo; OBS!<br></div>")
        Response.Write("<div id='error_invoiceDateMonth' class='edit_error'>&laquo; OBS!<br></div>")
        Response.Write("<div id='error_invoiceDateDay' class='edit_error'>&laquo; OBS!<br></div>")
        Response.Write("</td>")
        Response.Write("</tr>")
        If Len(invoiceCreditID) > 0 Then
            Response.Write("<tr><td align='right' colspan='2'><input type='submit' value='Save'></td></tr>")
        End If
        Response.Write("</form>")
        Response.Write("<tr><td colspan='3'><hr class='hrHeadline'></td></tr>")
        Response.Write("</table>")
        Response.Write("</td>")
        Response.Write("<td width='100'></td>")
        Response.Write("<td valign='top'>")
        Response.Write("<table cellpadding='2' cellspacing='2' width='450'>")
        Response.Write("<tr><td class='headline' colspan='4'>QUEUED INVOICES<hr class='hrHeadline'></td></tr>")
        dbOpen()
        RecSet = Conn.Execute("Select ic.invoiceCreditID, p.partnerName, c.country From ((tblInvoiceCredit ic INNER JOIN tblPartner p ON p.partnerID = ic.partnerID) INNER JOIN tblCountry c ON c.countryID = ic.countryID) Where ic.invoiceAmount = 0 AND ic.invoicePrinted = 0")
        If RecSet.EOF Then
            Response.Write("<tr><td><font color='red'>No invoices found.</font></td></tr>")
        Else
            Response.Write("<tr bgcolor='#eeeeee'>")
            Response.Write("<td class='formBold'>Invoice ID</td>")
            Response.Write("<td class='formBold'>Partner</td>")
            Response.Write("<td class='formBold'>Country</td>")
            Response.Write("<td class='formBold'></td>")
            Response.Write("</tr>")
            Do Until RecSet.EOF
                invoiceCreditID = RecSet.Fields("invoiceCreditID").Value
                partnerName = RecSet.Fields("partnerName").Value
                country = RecSet.Fields("country").Value
                If rowNumber = 0 Then
                    strGrey = " bgcolor='#eeeeee'"
                    rowNumber = 1
                Else
                    strGrey = ""
                    rowNumber = 0
                End If
                Response.Write("<tr" & strGrey & ">")
                Response.Write("<td>" & invoiceCreditID & "</td>")
                Response.Write("<td algin='right'>" & partnerName & "</td>")
                Response.Write("<td algin='right'>" & country & "</td>")
                Response.Write("<td algin='right'><a href='activateInvoiceCredit.aspx?invoiceCreditID=" & invoiceCreditID & "'>activate</a></td>")
                Response.Write("</tr>")
                RecSet.MoveNext()
            Loop
        End If
        RecSet.Close()
        RecSet = Nothing
        dbClose()
        Response.Write("<tr><td colspan='4'><hr class='hrHeadline'></td></tr>")
        Response.Write("</table>")
        Response.Write("</td>")
        Response.Write("</tr>")
        Response.Write("</table>")
        Under()
    ElseIf Request.QueryString("do") = "save" Then
        invoiceDateYear = Request.Form("invoiceDateYear")
        invoiceDateMonth = Request.Form("invoiceDateMonth")
        invoiceDateDay = Request.Form("invoiceDateDay")
        paymentTerm = Request.Form("paymentTerm")
        invoiceDateYear = CLng(invoiceDateYear)
        invoiceDateMonth = CLng(invoiceDateMonth)
        invoiceDateDay = CLng(invoiceDateDay)
        paymentTerm = CLng(paymentTerm)
        If CLng(invoiceDateMonth) < 10 Then
            invoiceDateMonth = "0" & invoiceDateMonth & ""
        End If
        If CLng(invoiceDateDay) < 10 Then
            invoiceDateDay = "0" & invoiceDateDay & ""
        End If
        invoiceDate = invoiceDateYear & "-" & invoiceDateMonth & "-" & invoiceDateDay
        expiryDate = DateAdd("d", CDbl(paymentTerm), Convert.ToDateTime(invoiceDate))
        dbOpen()
        RecSet = Conn.Execute("Select SUM(p.price) As invoiceAmount From (tblInvoiceCreditContent icc INNER JOIN tblPriceList p ON p.priceListID = icc.priceListID) Where icc.invoiceCreditID = " & invoiceCreditID & "")
        If RecSet.EOF Then
            Response.Redirect("activateCreditInvoice.aspx")
        Else
            invoiceAmount = RecSet.Fields("invoiceAmount").Value
        End If
        RecSet.Close()
        RecSet = Nothing
        dbClose()
        invoiceAmount = Replace(CStr(invoiceAmount), ",", ".")
        invoiceClaim = invoiceAmount
        userID = fragusLeasingUserID
        dbOpen()
        Response.Write("Update tblInvoiceCredit Set userID = " & userID & ", invoiceAmount = " & invoiceAmount & ", invoiceClaim = " & invoiceClaim & ", invoiceDate = '" & invoiceDate & "', expiryDate = '" & expiryDate & "', paymentTerm = " & paymentTerm & " Where invoiceCreditID = " & invoiceCreditID & "<br>")
        Conn.Execute("Update tblInvoiceCredit Set userID = " & userID & ", invoiceAmount = " & invoiceAmount & ", invoiceClaim = " & invoiceClaim & ", invoiceDate = '" & invoiceDate & "', expiryDate = '" & expiryDate & "', paymentTerm = " & paymentTerm & " Where invoiceCreditID = " & invoiceCreditID & "")
        dbClose()
        Response.Redirect("activateInvoiceCredit.aspx?subDo=%20-%20ACTIVATED")
    End If
%>
